package com.hundsun.bible.facade.impl.account;

import org.springframework.transaction.annotation.Transactional;

import com.hundsun.bible.domain.Constants;
import com.hundsun.bible.domain.Enum.EnumSubTransCode;
import com.hundsun.bible.domain.model.PointsAmount;
import com.hundsun.bible.domain.model.acctrans.AccountTransReq;
import com.hundsun.bible.domain.model.acctrans.AccountTransResult;
import com.hundsun.bible.domain.model.acctrans.FreezeBalanceReq;
import com.hundsun.bible.domain.util.Money;
import com.hundsun.bible.facade.account.PointsAmountManager;
import com.hundsun.bible.facade.acctrans.AccountTransManager;
import com.hundsun.bible.facade.user.UserPointsManager;

public class PointsAmountManagerImpl implements PointsAmountManager {
    private UserPointsManager   userPointsManager;
    private AccountTransManager accountTransManager;

    public UserPointsManager getUserPointsManager() {
        return userPointsManager;
    }

    public void setUserPointsManager(UserPointsManager userPointsManager) {
        this.userPointsManager = userPointsManager;
    }

    public AccountTransManager getAccountTransManager() {
        return accountTransManager;
    }

    public void setAccountTransManager(AccountTransManager accountTransManager) {
        this.accountTransManager = accountTransManager;
    }

    //积分转余额
    @Transactional
    public String pointsAmountSubS(PointsAmount pointsAmount) {
        String statusMessage = "0";
        //先执行积分扣减，积分扣减成功后执行余额增加
        if (userPointsManager.toCash(pointsAmount.getUserId(), Integer.parseInt(pointsAmount
            .getTransAmountStr()), "")) {
            AccountTransReq ReqFreeze = new FreezeBalanceReq();
            ReqFreeze.setInAccountNo(pointsAmount.getAccountNo());
            ReqFreeze.setAmount(new Money(Double.valueOf(pointsAmount.getTransAmountStr())
                                          / (100 * Constants.CENT_TO_POINT)));
            ReqFreeze.setSubTransCode(EnumSubTransCode.TXCODE_TRANSFER_POINT_TO_BALANCE);
            AccountTransResult result = accountTransManager.execute(ReqFreeze);
            if (result != AccountTransResult.TXN_RESULT_SUCCESS) {
                statusMessage = result.getMessage();
            }
        } else {
            statusMessage = "积分扣减失败！";
        }
        return statusMessage;
    }

}
